home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / sc3x03.exe / CONNOPEN.C < prev    next >
C/C++ Source or Header  |  1992-10-09  |  11KB  |  286 lines

  1. //   IMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM;
  2. //   :                                                                    :
  3. //   : module:      connopen.c                                            :
  4. //   : abstract:    This module shows how to make 3.x system calls using  :
  5. //   :              the F2 Shell Interface for the GetConnectionsOpen     :
  6. //   :              Files API, obviously it requires the NetWare Shell.   :
  7. //   :                                                                    :
  8. //   :              This call may need to be made iteratively to return   :
  9. //   :              all of the restriction information.  For simplicity,  :
  10. //   :              this example only makes the call once.                :
  11. //   :                                                                    :
  12. //   : environment: NetWare 3.x v3.11                                     :
  13. //   :              Borland C 3.0                                         :
  14. //   :                                                                    :
  15. //   :  This software is provided as is and carries no warranty           :
  16. //   :  whatsoever.  Novell disclaims and excludes any and all implied    :
  17. //   :  warranties of merchantability, title and fitness for a particular :
  18. //   :  purpose.  Novell does not warrant that the software will satisfy  :
  19. //   :  your requirements or that the software is without defect or error :
  20. //   :  or that operation of the software will be uninterrupted.  You are :
  21. //   :  using the software at your risk.  The software is not a product   :
  22. //   :  of Novell, Inc. or any of subsidiaries.                           :
  23. //   HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM<
  24. //
  25. //                         ****** N O T I C E ******
  26. //
  27. //     This API call can cause a server abend if a connection number is
  28. //     used that is greater than what the OS supports.
  29. //
  30. //     This software is considered pre-release and may be used at your own
  31. //     risk and has been provided due to the many requests of our cust-
  32. //     omers.  Support for this module will be provided at the sole
  33. //     discretion of Novell, Inc.
  34. //
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <conio.h>
  40. #include <dos.h>
  41. #include <fcntl.h>
  42. #include <sys\types.h>
  43. #include <sys\stat.h>
  44.  
  45. #include "nwsys.h"
  46.  
  47. //
  48. //  Since the file names are returned length preceded, the file name is
  49. //  not a constant 12 or 13 bytes.  The maximum names that can be returned
  50. //  is based upon a maximum reply buffer length of 512 bytes.
  51. //
  52.  
  53. #define MAX_RETURNED 27
  54.  
  55. //
  56. //  First of all, we define the request and reply structures which are
  57. //  needed for the GetConnectionsOpenFiles API call.  These structures are
  58. //  layed out according to the System Call documentation.
  59. //
  60.  
  61. struct  REQUEST {
  62.      WORD    sflen;                 // length of the structure
  63.      BYTE    sfcode;                // the subfunction code
  64.      WORD    connNumber;            // target connection number
  65.      WORD    lastRecordSeen;        // last record returned, i.e. sequence
  66. }Request;
  67.  
  68. struct fileStruct{
  69.      WORD    taskNumber;            // task number within ws holding file open
  70.      BYTE    lockType;              // lock type, ie sharable, logged, locked, TTS
  71.      BYTE    accessControl;         // access rights to file
  72.      BYTE    lockFlag;              // lock flag
  73.      BYTE    volumeNumber;          // volume number file resides upon
  74.      LONG    parentDirEntryNumber;  // parent directory entry seq number
  75.      LONG    dirEntryNumber;        // directory entry seq number
  76.      BYTE    reserved[1];           // reserved for future use
  77.      BYTE    nameSpace;             // name space identifier
  78.      BYTE    fileName[13];          // name of file (null terminated)
  79. };
  80.  
  81. struct   fileStruct *fs[MAX_RETURNED];
  82.                                               // array of pointers to file info
  83. int   numOpenFiles;                // number of open files
  84. WORD  connectionNumber;            // connection number to query
  85.  
  86. struct  {
  87.      WORD    nextRequestRec; // next record to be requested.
  88.      WORD    numberReturned; // number of records returned
  89.      BYTE    buffer[508];    // raw reply data
  90. }Reply;
  91.  
  92. void ProcessEntries(void);
  93. void FreeEntries(void);
  94. void DisplayData(void);
  95. int GetPath(int, BYTE *);
  96. int GetVolumeName(BYTE *, BYTE *);
  97. void NetWarePathToDOSPath(BYTE *);
  98. //
  99. //  This routine allocates memory for the data returned for each file.
  100. //  It also makes the GetConnectionOpenFiles call and parses the reply
  101. //  buffer.
  102. //
  103. void ProcessEntries(void)
  104. {
  105.     char *lookup;                          // pointer for the reply buffer
  106.     int i,j;                        // iteration counters
  107.  
  108.     lookup=(char *)&Reply.buffer;
  109.     numOpenFiles=Reply.numberReturned;
  110.     if(numOpenFiles > MAX_RETURNED) {
  111.         printf("\nToo many files returned!  Must ABORT");
  112.         return;
  113.     }
  114.     for(i=0;i < numOpenFiles; i++) {
  115.         fs[i]=malloc(sizeof (struct fileStruct));
  116.         memmove((char *)&fs[i]->taskNumber,(lookup),2);
  117.         fs[i]->lockType=*(lookup+2);
  118.         fs[i]->accessControl=*(lookup+3);
  119.         fs[i]->lockFlag=*(lookup+4);
  120.         fs[i]->volumeNumber=*(lookup+5);
  121.         memmove((char *)&fs[i]->parentDirEntryNumber,(lookup+6),4);
  122.         memmove((char *)&fs[i]->dirEntryNumber,(lookup+10),4);
  123.         memmove(fs[i]->reserved,(lookup+14),1);
  124.         fs[i]->nameSpace=*(lookup+15);
  125.         for(j=0;j<*(lookup+16);j++)
  126.             fs[i]->fileName[j]=*(lookup+17+j);
  127.         fs[i]->fileName[j]='\0';
  128.         lookup=(lookup+17+j);
  129.     }
  130. }
  131.  
  132. //
  133. //  This routine frees memory allocated by ProcessEntries.
  134. //
  135. void FreeEntries( void )
  136. {
  137.     int i;                          // iteration counter
  138.  
  139.     for(i=0;i<numOpenFiles;i++)
  140.         free(fs[i]);
  141. }
  142.  
  143. //
  144. //  Display the list of open files for the connection.
  145. //
  146. void DisplayData( void )
  147. {
  148.     int i;                          // iteration counter
  149.     int cc;                         // completion code
  150.     char path[255];                 // returned path
  151.  
  152.     printf("\n\nList Of %d Open Files For Connection %d\n\n",
  153.         numOpenFiles,
  154.         connectionNumber);
  155.     for(i=0;i<numOpenFiles;i++) {
  156.         cc=GetPath(i,path);
  157.         if( !cc ) printf("%s/%s\n",path,&fs[i]->fileName);
  158.     }
  159. }
  160.  
  161. //
  162. //  This function will obtain the full volume NetWare path of a specified
  163. //  volume number, directory entry number and name space set.  This function
  164. //  calls the MapDirectoryNumberToPath and GetVolumeName NetWare APIs.
  165. //
  166. int GetPath(int offset, BYTE *directoryPath)
  167. {
  168.     static struct {
  169.         WORD    sflen;               // length of the structure
  170.         BYTE    sfcode;              // the subfunction code
  171.         BYTE    volumeNumber;        // volume number
  172.         LONG    dirEntryNumber;      // directory entry sequence number
  173.         BYTE    nameSpace;           // name space
  174.     } GetPathRequest;               // MapDirectoryNumberToPath request
  175.                                               // structure
  176.     static struct {
  177.         BYTE    directoryPath[255];  // NetWarePath (not the same as DOS path)
  178.     } GetPathReply;                 // MapDirectoryNumberToPath reply
  179.                                               // structure
  180.     BYTE volumeName[16];            // volume name
  181.     int cc;                         // completion code
  182.  
  183.     memset(&GetPathRequest,'\0',sizeof GetPathRequest);
  184.     memset(&GetPathReply,'\0',sizeof GetPathReply);
  185.     GetPathRequest.sflen = (sizeof GetPathRequest) ;
  186.     GetPathRequest.sfcode = 243;                       // subfunction code
  187.     GetPathRequest.volumeNumber=fs[offset]->volumeNumber;
  188.     GetPathRequest.dirEntryNumber=fs[offset]->parentDirEntryNumber;
  189.     GetPathRequest.nameSpace=fs[offset]->nameSpace;
  190.     cc = NWSystemCall(23,&GetPathRequest,sizeof GetPathRequest,
  191.             &GetPathReply,sizeof GetPathReply);
  192.     if(cc) {
  193.         printf("MapDirectoryNumberToPath returned:    %03d--%#02x\n",cc,cc);
  194.         return(cc);
  195.     }
  196.     cc=GetVolumeName(&fs[offset]->volumeNumber,volumeName);
  197.     if(cc) {
  198.         printf("GetVolumeName returned:    %03d--%#02x\n",cc,cc);
  199.         return(cc);
  200.     }
  201.     NetWarePathToDOSPath(GetPathReply.directoryPath);
  202.     strcpy(directoryPath,volumeName);
  203.     strcat(directoryPath,GetPathReply.directoryPath);
  204.     return(0);
  205. }
  206.  
  207. //
  208. //  This function will return the name of the specified volume.
  209. //
  210.  
  211. int     GetVolumeName(BYTE *volumeNumber, BYTE *volumeName)
  212. {
  213.     struct {
  214.         WORD    sflen;               // length of the structure
  215.         BYTE    sfcode;              // the subfunction code
  216.         BYTE    volumeNumber;        // number of volume
  217.     } VolumeNameRequest;            // GetVolumeName request structure
  218.  
  219.     struct {
  220.         BYTE    volumeNameLength;    // volume name length
  221.         BYTE    volumeName[255];     // volume name (not terminated)
  222.     } VolumeNameReply;              // GetVolumeName reply structure
  223.  
  224.     int     cc;                     // completion code
  225.     int volumeNameLength;           // length of volume name
  226.  
  227.     memset(&VolumeNameRequest,'\0',sizeof VolumeNameRequest);
  228.     memset(&VolumeNameReply,'\0',sizeof VolumeNameReply);
  229.     VolumeNameRequest.sflen = (sizeof VolumeNameRequest) ;
  230.     VolumeNameRequest.sfcode = 06;                       // subfunction code
  231.     VolumeNameRequest.volumeNumber=*volumeNumber;
  232.     cc=NWSystemCall(22,&VolumeNameRequest,sizeof VolumeNameRequest,
  233.             &VolumeNameReply,sizeof VolumeNameReply);
  234.     if(cc) return(cc);
  235.     volumeNameLength=(int)VolumeNameReply.volumeNameLength;
  236.     memmove(volumeName,VolumeNameReply.volumeName,volumeNameLength);
  237.     volumeName[volumeNameLength]='\0';
  238.     return(0);
  239. }
  240.  
  241. //
  242. //  Convert a NetWare-Style path to a DOS-Style path, in place
  243. //
  244.  
  245. void    NetWarePathToDOSPath( BYTE *psp )
  246. {
  247.     LONG    componentLen;
  248.  
  249.     componentLen = *psp;            // remember the length
  250.     *psp++ = ':';                   // put volume separator
  251.     psp += componentLen;            // get to next component
  252.     while( *psp != '\0' ){
  253.         componentLen = *psp;         // remember length
  254.         *psp = '/';                  // put directory separator
  255.         psp += componentLen + 1;     // to next component
  256.     }
  257.     *psp = NULL;                    // put null terminator
  258. }
  259.  
  260. //
  261. //  This is the main program.  The purpose is to make the GetConnections
  262. //  OpenFiles API call to illustrate making system calls using the F2
  263. //  interface.
  264. //
  265.  
  266. void main(int argc, char *argv[])
  267. {
  268.     int     cc;
  269.     if(argc!=2){printf("usage: CONNOPEN connectionNumber\n");exit(1);}
  270.     connectionNumber=atoi(argv[1]);
  271. //
  272. //  Build the request buffer
  273. //
  274.     Request.sflen = (sizeof Request) ;
  275.     Request.sfcode = 235;                       // subfunction code
  276.     Request.connNumber = connectionNumber;
  277.     Request.lastRecordSeen = 0;
  278.     cc = NWSystemCall(23,&Request,sizeof Request,&Reply,sizeof Reply);
  279.     if( cc ) printf("GetConnectionsOpenFiles returned:    %03d--%#02x\n",cc,cc);
  280.     else {
  281.         ProcessEntries();
  282.         DisplayData();
  283.         FreeEntries();
  284.     }
  285. }
  286.